package com.webframe.web.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class BaseController
{
	private final Log	logger	= LogFactory.getLog(getClass());
	/**
	 * 输出文本字符串
	 * 
	 * @param res
	 * @param msg
	 */
	protected void outWrite(HttpServletResponse res, String msg) {
		outWrite(res, msg, "text/xml");
	}

	/**
	 * 输出xml
	 * 
	 * @param res
	 * @param xml
	 */
	protected void outWriteXml(HttpServletResponse res, String xml) {
		outWrite(res, xml, "application/xml");
	}

	/**
	 * 输出JSON
	 * 
	 * @param res
	 * @param json
	 */
	protected void outWriteJSON(HttpServletResponse res, String json) {
		outWrite(res, json, "application/json");
	}
	/**
	 * 返回流写入信息，并可以指定内容类型
	 * 
	 * @param res 返回流
	 * @param msg 内容
	 * @param contentType 内容类型
	 */
	protected void outWrite(HttpServletResponse res, String msg, String contentType) {
		res.setCharacterEncoding("utf-8");
		res.setContentType(contentType);
		PrintWriter out = null;
		try {
			out = res.getWriter();
			out.write(msg);
			out.flush();
		} catch (IOException e) {
			logger.error(e);
		}
		finally
		{
			if(out != null)
			{
				out.close();
			}
		}
	}
}
